package com.baozun.houji.order.widget

import android.app.Activity
import android.view.View
import com.baozhun.mall.common.listener.ViewOnClickListener
import com.baozhun.mall.common.widget.dialog.BaseBottomPopupView
import com.baozun.houji.order.R
import com.baozun.houji.order.databinding.DialogChoosePayWayBinding
import com.baozun.houji.order.viewmodel.OrderListViewModel
import com.common.pay.PayType
import com.common.pay.PayUtils


/**
 * @Title：选择支付方式dialog
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/21
 * @Version：1.0.0
 */
class ChoosePayWayDialog(
    var context: Activity,
    var orderId: String,
    val amount: String,
    var paySuccess: () -> Unit
) : BaseBottomPopupView<DialogChoosePayWayBinding>(context), ViewOnClickListener {

    private val requestViewModel: OrderListViewModel by lazy { OrderListViewModel() }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_choose_pay_way
    }

    override fun onCreate() {
        super.onCreate()
        mBinding.amount = amount
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_close -> {
                dismiss()
            }
            R.id.tv_confirm_pay -> {
                var payType = if (mBinding.includePayWay.rgPayWay.checkedRadioButtonId == com.baozhun.mall.common.R.id.rb_alipay) PayType.ALIPAY else PayType.WX_PAY
                requestViewModel.orderPay(orderId, payType.type) {
                    dismiss()
                    PayUtils.getInstance(context).pay(payType, it, object : PayUtils.PayListener {
                        override fun onPaySuccess() {
                            paySuccess.invoke()
                        }

                        override fun onPayError(errorCode: Int, message: String?) {
                        }
                    })
                }

            }
        }
    }
}